from extensions import db
from datetime import datetime
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash

# 用户模型
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True, nullable=False)
    password_hash = db.Column(db.String(128), nullable=False)
    role = db.Column(db.String(20), nullable=False, default='author')  # 角色：admin 或 author
    name = db.Column(db.String(100), nullable=True)  # 用户姓名
    email = db.Column(db.String(100), unique=True)  # 邮箱
    bio = db.Column(db.Text)  # 个人简介
    avatar = db.Column(db.String(255))  # 头像地址
    website = db.Column(db.String(255))  # 个人网站
    github = db.Column(db.String(255))  # GitHub链接
    twitter = db.Column(db.String(255))  # Twitter链接
    is_active = db.Column(db.Boolean, default=True)  # 是否激活
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    def set_password(self, password):
        self.password_hash = generate_password_hash(password)
        
    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

# 文章分类模型
class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)  # 分类名称
    description = db.Column(db.Text)  # 分类描述
    color = db.Column(db.String(7), default='#007bff')  # 分类颜色
    parent_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=True)  # 父分类ID
    sort_order = db.Column(db.Integer, default=0)  # 排序
    is_active = db.Column(db.Boolean, default=True)  # 是否激活
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 自关联关系
    children = db.relationship('Category', backref=db.backref('parent', remote_side=[id]), lazy=True)
    posts = db.relationship('Post', backref='category', lazy=True)

# 标签模型
class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), unique=True, nullable=False)  # 标签名称
    description = db.Column(db.Text)  # 标签描述
    color = db.Column(db.String(7), default='#6c757d')  # 标签颜色
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 多对多关系
    posts = db.relationship('Post', secondary='post_tag', backref='tags', lazy=True)

# 文章模型
class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)  # 文章标题
    content = db.Column(db.Text, nullable=False)  # 文章内容
    excerpt = db.Column(db.Text)  # 文章摘要
    cover_image = db.Column(db.String(255))  # 封面图片
    layout_mode = db.Column(db.String(20), default='no-image')  # 布局模式：no-image, single-image, three-image
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)  # 作者ID
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=True)  # 分类ID
    status = db.Column(db.String(20), default='draft')  # 状态：draft, published, archived
    is_featured = db.Column(db.Boolean, default=False)  # 是否精选
    allow_comments = db.Column(db.Boolean, default=True)  # 是否允许评论
    view_count = db.Column(db.Integer, default=0)  # 浏览次数
    like_count = db.Column(db.Integer, default=0)  # 点赞次数
    comment_count = db.Column(db.Integer, default=0)  # 评论次数
    html_path = db.Column(db.String(500))  # 静态HTML文件路径
    published_at = db.Column(db.DateTime)  # 发布时间
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 关联关系
    author = db.relationship('User', backref='posts', lazy=True)
    comments = db.relationship('Comment', backref='post', lazy=True, cascade='all, delete-orphan')

# 文章标签关联表
class PostTag(db.Model):
    __tablename__ = 'post_tag'
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'), primary_key=True)
    tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'), primary_key=True)

# 评论模型
class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)
    author_name = db.Column(db.String(100), nullable=False)  # 评论者姓名
    author_email = db.Column(db.String(100), nullable=False)  # 评论者邮箱
    author_website = db.Column(db.String(255))  # 评论者网站
    content = db.Column(db.Text, nullable=False)  # 评论内容
    parent_id = db.Column(db.Integer, db.ForeignKey('comment.id'), nullable=True)  # 父评论ID
    status = db.Column(db.String(20), default='pending')  # 状态：pending, approved, rejected
    ip_address = db.Column(db.String(45))  # IP地址
    user_agent = db.Column(db.String(255))  # 用户代理
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 自关联关系（回复评论）
    replies = db.relationship('Comment', backref=db.backref('parent', remote_side=[id]), lazy=True)

# 系统设置模型
class Setting(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    key = db.Column(db.String(100), unique=True, nullable=False)  # 设置键
    value = db.Column(db.Text)  # 设置值
    description = db.Column(db.Text)  # 设置描述
    type = db.Column(db.String(20), default='string')  # 类型：string, int, bool, json
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
